﻿using System.Data;
using IceCS.GoodsSale.Models.UserModels;
using Microsoft.Data.SqlClient;

namespace IceCS.GoodsSale.DAL
{
    public class UserPswProInfoAccesser
    {
        DBHelper dh = new DBHelper();
        PublicMethodsAccesser publicMethodsDAL = new PublicMethodsAccesser();

        //添加密码保护信息的方法
        public bool InsertUserPswProInfo(UserPswProInfo userPswProInfo)
        {
            string enName = publicMethodsDAL.EnCode(userPswProInfo.Name);
            string enHometown = publicMethodsDAL.EnCode(userPswProInfo.Hometown);
            string enPhoneNumber = publicMethodsDAL.EnCode(userPswProInfo.PhoneNumber);
            string enEMail = publicMethodsDAL.EnCode(userPswProInfo.EMail);
            string sqlStr = "insert into TB_UserPswProInfo values(@UserName,@Name,@Hometown,@PhoneNumber,@EMail)";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@UserName",userPswProInfo.UserName),
                new SqlParameter("@Name",enName),
                new SqlParameter("@Hometown",enHometown),
                new SqlParameter("@PhoneNumber",enPhoneNumber),
                new SqlParameter("@EMail",enEMail),
            };
            return dh.ExcuteCommand(sqlStr, param);
        }

        //删除密码保护信息的方法
        public bool DeleteUserPswProInfo(string userName)
        {
            string sqlStr = "delete from TB_UserPswProInfo where UserName='" + userName + "'";
            return dh.ExcuteCommand(sqlStr);
        }

        //修改密码保护信息的方法
        public bool UpdateUserPswProInfo(UserPswProInfo userPswProInfo)
        {
            string enName = publicMethodsDAL.EnCode(userPswProInfo.Name);
            string enHometown = publicMethodsDAL.EnCode(userPswProInfo.Hometown);
            string enPhoneNumber = publicMethodsDAL.EnCode(userPswProInfo.PhoneNumber);
            string enEMail = publicMethodsDAL.EnCode(userPswProInfo.EMail);
            string sqlStr = "update TB_UserPswProInfo set Name=@Name,Hometown=@Hometown,PhoneNumber=@PhoneNumber,EMail=@EMail where UserName=@UserName";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@Name",enName),
                new SqlParameter("@Hometown",enHometown),
                new SqlParameter("@PhoneNumber",enPhoneNumber),
                new SqlParameter("@EMail",enEMail),
                new SqlParameter("@UserName",userPswProInfo.UserName),
            };
            return dh.ExcuteCommand(sqlStr, param);
        }

        //查询指定用户的密码保护信息的方法
        public UserPswProInfo SelectUserPswProInfo(string userName)
        {
            string sqlStr = "select Name,Hometown,PhoneNumber,EMail from TB_UserPswProInfo where UserName='" + userName + "'";
            DataTable dt = dh.GetDataTable(sqlStr);
            if (dt.Rows.Count != 0)
            {
                UserPswProInfo userPswProInfo = new UserPswProInfo();
                userPswProInfo.Name = publicMethodsDAL.DeCode(dt.Rows[0].ItemArray[0].ToString());
                userPswProInfo.Hometown = publicMethodsDAL.DeCode(dt.Rows[0].ItemArray[1].ToString());
                userPswProInfo.PhoneNumber = publicMethodsDAL.DeCode(dt.Rows[0].ItemArray[2].ToString());
                userPswProInfo.EMail = publicMethodsDAL.DeCode(dt.Rows[0].ItemArray[3].ToString());
                return userPswProInfo;
            }
            else
            {
                return null;
            }
        }
    }
}
